package com.dianping.cat.home.utilization.transform;

import com.dianping.cat.home.utilization.entity.ApplicationState;
import com.dianping.cat.home.utilization.entity.Domain;
import com.dianping.cat.home.utilization.entity.MachineState;
import com.dianping.cat.home.utilization.entity.UtilizationReport;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.xml.sax.Attributes;












public class DefaultSaxMaker
  extends Object
  implements IMaker<Attributes>
{
  public ApplicationState buildApplicationState(Attributes attributes) {
    String id = attributes.getValue("id");
/*  30 */     String failureCount = attributes.getValue("failure-count");
    String failurePercent = attributes.getValue("failure-percent");
    String count = attributes.getValue("count");
/*  33 */     String maxQps = attributes.getValue("maxQps");
/*  34 */     String avg = attributes.getValue("avg");
/*  35 */     String sum = attributes.getValue("sum");
    String avg95 = attributes.getValue("avg95");
/*  37 */     ApplicationState applicationState = new ApplicationState(id);

/*  39 */     if (failureCount != null) {
/*  40 */       applicationState.setFailureCount(((Long)convert(Long.class, failureCount, Long.valueOf(0L))).longValue());
    }
    
/*  43 */     if (failurePercent != null) {
/*  44 */       applicationState.setFailurePercent(toNumber(failurePercent, "0.000000", Integer.valueOf(0)).doubleValue());
    }
    
/*  47 */     if (count != null) {
/*  48 */       applicationState.setCount(((Long)convert(Long.class, count, Long.valueOf(0L))).longValue());
    }
    
/*  51 */     if (maxQps != null) {
/*  52 */       applicationState.setMaxQps(toNumber(maxQps, "0.0000", Integer.valueOf(0)).doubleValue());
    }
    
/*  55 */     if (avg != null) {
/*  56 */       applicationState.setAvg(toNumber(avg, "0.0000", Integer.valueOf(0)).doubleValue());
    }
    
/*  59 */     if (sum != null) {
/*  60 */       applicationState.setSum(toNumber(sum, "0.0000", Integer.valueOf(0)).doubleValue());
    }
    
/*  63 */     if (avg95 != null) {
/*  64 */       applicationState.setAvg95(toNumber(avg95, "0.0000", Integer.valueOf(0)).doubleValue());
    }
    
/*  67 */     return applicationState;
  }

  
  public Domain buildDomain(Attributes attributes) {
/*  72 */     String id = attributes.getValue("id");
/*  73 */     String machineNumber = attributes.getValue("machine-number");
/*  74 */     String cmdbId = attributes.getValue("cmdb-id");
/*  75 */     Domain domain = new Domain(id);

/*  77 */     if (machineNumber != null) {
      domain.setMachineNumber(((Integer)convert(Integer.class, machineNumber, Integer.valueOf(0))).intValue());
    }
    
/*  81 */     if (cmdbId != null) {
/*  82 */       domain.setCmdbId(cmdbId);
    }
    
/*  85 */     return domain;
  }

  
  public MachineState buildMachineState(Attributes attributes) {
/*  90 */     String id = attributes.getValue("id");
/*  91 */     String count = attributes.getValue("count");
/*  92 */     String sum = attributes.getValue("sum");
/*  93 */     String avg = attributes.getValue("avg");
/*  94 */     String avgMax = attributes.getValue("avg-max");
    MachineState machineState = new MachineState(id);

/*  97 */     if (count != null) {
/*  98 */       machineState.setCount(((Long)convert(Long.class, count, Long.valueOf(0L))).longValue());
    }
    
/* 101 */     if (sum != null) {
/* 102 */       machineState.setSum(toNumber(sum, "0.0000", Integer.valueOf(0)).doubleValue());
    }
    
/* 105 */     if (avg != null) {
/* 106 */       machineState.setAvg(toNumber(avg, "0.0000", Integer.valueOf(0)).doubleValue());
    }
    
/* 109 */     if (avgMax != null) {
/* 110 */       machineState.setAvgMax(toNumber(avgMax, "0.0000", Integer.valueOf(0)).doubleValue());
    }
    
/* 113 */     return machineState;
  }

  
  public UtilizationReport buildUtilizationReport(Attributes attributes) {
/* 118 */     String domain = attributes.getValue("domain");
/* 119 */     String startTime = attributes.getValue("startTime");
/* 120 */     String endTime = attributes.getValue("endTime");
/* 121 */     UtilizationReport utilizationReport = new UtilizationReport(domain);

/* 123 */     if (startTime != null) {
/* 124 */       utilizationReport.setStartTime(toDate(startTime, "yyyy-MM-dd HH:mm:ss", null));
    }
    
/* 127 */     if (endTime != null) {
/* 128 */       utilizationReport.setEndTime(toDate(endTime, "yyyy-MM-dd HH:mm:ss", null));
    }
    
/* 131 */     return utilizationReport;
  }

  
  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/* 136 */     if (value == null || value.length() == 0) {
/* 137 */       return defaultValue;
    }
    
/* 140 */     if (type == Boolean.class || type == boolean.class)
/* 141 */       return (T)Boolean.valueOf(value); 
/* 142 */     if (type == Integer.class || type == int.class)
/* 143 */       return (T)Integer.valueOf(value); 
/* 144 */     if (type == Long.class || type == long.class)
/* 145 */       return (T)Long.valueOf(value); 
/* 146 */     if (type == Short.class || type == short.class)
/* 147 */       return (T)Short.valueOf(value); 
/* 148 */     if (type == Float.class || type == float.class)
/* 149 */       return (T)Float.valueOf(value); 
/* 150 */     if (type == Double.class || type == double.class)
/* 151 */       return (T)Double.valueOf(value); 
/* 152 */     if (type == Byte.class || type == byte.class)
/* 153 */       return (T)Byte.valueOf(value); 
/* 154 */     if (type == Character.class || type == char.class) {
/* 155 */       return (T)Character.valueOf(value.charAt(0));
    }
/* 157 */     return (T)value;
  }

  
  protected Date toDate(String str, String format, Date defaultValue) {
/* 162 */     if (str == null || str.length() == 0) {
/* 163 */       return defaultValue;
    }
    
    try {
/* 167 */       return (new SimpleDateFormat(format)).parse(str);
/* 168 */     } catch (ParseException e) {
/* 169 */       throw new RuntimeException(String.format("Unable to parse date(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }
  
  protected Number toNumber(String str, String format, Number defaultValue) {
/* 174 */     if (str == null || str.length() == 0) {
/* 175 */       return defaultValue;
    }
    
    try {
/* 179 */       return (new DecimalFormat(format)).parse(str);
/* 180 */     } catch (ParseException e) {
/* 181 */       throw new RuntimeException(String.format("Unable to parse number(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/utilization/transform/DefaultSaxMaker.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */